package com.kamistoat.meimeimall.meimeimallcart.dubbo;

import com.alibaba.fastjson.JSON;
import com.kamistoat.meimeimall.common.To.cartTo.CartDeleteTo;
import com.kamistoat.meimeimall.common.To.orderTo.OrderItemTo;
import com.kamistoat.meimeimall.common.To.wareTo.LockOrderItemWareTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.dubboInterface.CartDubboInterface;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallcart.service.CartService;
import io.seata.core.constants.DubboConstants;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Dubbo接口实现.
 */
@Service
@Component
public class CartDubboInterfaceImpl implements CartDubboInterface {

    @Autowired
    CartService cartService;

    /**
     * 获取用户购物车中所有的选中商品
     *
     * @return R对象 内含 "orderItemToList" - List<OrderItemTo>
     */
    @DubboAndHTTPExceptionCatch
    @Override
    public R getOrderCartChecked() {
        List<OrderItemTo> orderItemToList = cartService.getOrderCartChecked();
        return R.ok().put("orderItemToList", JSON.toJSONString(orderItemToList));
    }

    /**
     * 下单成功后从用户购物车中删除这些选中项
     *
     * @param businessActionContext
     * @param lockOrderItemWareToList 下单的商品小项
     * @return R对象
     */
    @DubboAndHTTPExceptionCatch
    @Override
    public R deleteOrderItemInCart(BusinessActionContext businessActionContext, List<LockOrderItemWareTo> lockOrderItemWareToList) {
        try {
            cartService.deleteOrderItemInCart(lockOrderItemWareToList);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 购物车删除打钩项。分支事务提交。
     *
     * @param businessActionContext
     * @return
     */
    @DubboAndHTTPExceptionCatch
    @Override
    public boolean deleteOrderItemInCartCommit(BusinessActionContext businessActionContext) {
        // 由于购物车是用redis做的，所以提交动作简单
        return true;
    }

    /**
     * 购物车删除打钩项。分支事务回滚
     *
     * @param businessActionContext
     * @return
     */
    @DubboAndHTTPExceptionCatch
    @Override
    public boolean deleteOrderItemInCartRollback(BusinessActionContext businessActionContext) {
        List<LockOrderItemWareTo> lockOrderItemWareToList = (List<LockOrderItemWareTo>) businessActionContext.getActionContext("lockOrderItemWareToList");
        try{
            cartService.deleteOrderItemInCartRollback(lockOrderItemWareToList);
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    @Transactional
    public R deleteOrderItem(CartDeleteTo cartDeleteTo) {
        return null;
    }
}
